﻿using PSI.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PSI.Models;


namespace PSI.Controllers
{
    public class MassageController : Controller
    {
        //
        // GET: /Massage/

        public ActionResult Index()
        {
            ViewBag.UserName = UserService.GetLoginUserName();
            return View();
        }

        [HttpPost]
        public ActionResult GetMassageItems()
        {
            var data = new MassageService().GetList(false);

            return Json(new { Data = data });
        }

        [HttpPost]
        public ActionResult MinaMassageItems()
        {
            var data = new MassageService().GetList(true);

            return Json(new { Data = data });
        }


        [HttpPost]
        public ActionResult UploadFile()
        {
            try
            {
                var formname = this.Request.Form["filename"];
                var formtype = this.Request.Form["filetype"];

                var ty = formtype == "1" ? "file1" : "file2";

                var files = this.Request.Files[ty];

                var path = this.Server.MapPath("~");
                path += @"\upload_massage";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                var fileName = path + @"\" + formname;

                files.SaveAs(fileName);

                return Content("{success: true}");
            }
            catch (Exception ex)
            {
                return Content("{success: false, msg: '" + ex.Message + "'}");
            }

        }

        [HttpPost]
        public ActionResult ReleaseMassage(Massage model)
        {

            model.ReleasDate = DateTime.Now;
            model.ReleasePrason = UserService.GetLoginUserName();
            bool result=false;
            if (!string.IsNullOrEmpty(model.Id))
            {
                result = new Services.MassageService().EditMassage(model);
            }
            else
            {
                 result = new Services.MassageService().ReleaseMassage(model);
            }
            if (result)
            {
                return Content("{success:true}");
            }
            else
            {
                return Content("{success:false}");
            }
        }

        [HttpPost]
        public ActionResult DelMassage(string id)
        {
            var result = new MassageService().DelMassage(id);
            if (result)
            {
                return Content("{success:true}");
            }
            else
            {
                return Content("{success:false}");
            }
        }

        [HttpPost]
        public ActionResult Getsingle()
        { 
           var id=Request["id"].ToString();
            
           var dat = new MassageService().GetSinlge(id);

           return Json(new {Data=dat});          
        }

        public ActionResult DownLoad(string id, int num)
        {
            var path = this.Server.MapPath("~");
            path += @"upload_massage";
            var filename = "";
            var items = new MassageService().GetSinlge(id);
            if (num == 1)
            {
                path += @"\" + items.File1;
                var cc = (items.File1).Split('_');
                filename = cc[1];
            }
            else
            {
                path += @"\" + items.File2;
                var cc = (items.File2).Split('_');
                filename = cc[1];
            }

            return File(path, "application/vnd.ms-excel", filename);
        }
    }
}
